package com.ruike.alisurface.ui.call;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.ruike.alisurface.R;
import com.ruike.alisurface.base.BaseAty;
import com.ruike.alisurface.bean.CarGoodsBeans;
import com.ruike.alisurface.bean.ShopOrderBean;
import com.ruike.alisurface.https.ShopApiHttp;
import com.ruike.alisurface.ui.ShopMain_Aty;
import com.ruike.alisurface.ui.adapters.ShopPayAdapter;
import com.ruike.alisurface.utils.FinalDbUtils;
import com.ruike.alisurface.utils.GsonUtils;
import com.voodoo.lib_http.ApiListener;
import com.voodoo.lib_logcat.L;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 选择是否有无处方页
 */
public class CallTypeInquiry_Aty extends BaseAty implements ApiListener {


    @BindView(R.id.slecall_times)
    TextView authTimes;
    @BindView(R.id.slecall_shop_rcv)
    RecyclerView authShopRcv;


    @Override
    protected int getLayoutResId() {
        return R.layout.app_select_calltype;
    }

    @Override
    protected void initViews() {
        initTitleBar(false);
        startCountdownTimer(60, authTimes, ShopMain_Aty.class);
    }

    ShopOrderBean shopOrderBean;//订单数据
    List<CarGoodsBeans> goodsBeansList = new ArrayList<>();//商品信息集合

    @Override
    protected void initData() {
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            String json = getIntent().getStringExtra("shopOrderBean");
            shopOrderBean = GsonUtils.fromJson(json, ShopOrderBean.class);
        }
        goodsBeansList.addAll(FinalDbUtils.getInstance().findAll(CarGoodsBeans.class));
        ShopPayAdapter shopPayAdapter = new ShopPayAdapter(R.layout.pay_shop_list_item, goodsBeansList, this);
        authShopRcv.setLayoutManager(new LinearLayoutManager(this, RecyclerView.HORIZONTAL, false));
        authShopRcv.setAdapter(shopPayAdapter);
        L.i("订单信息==", goodsBeansList.toString());

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        goodsBeansList.clear();
        shopOrderBean = null;
    }

    String doctor_id;
    int consultation_type;

    @OnClick({R.id.slecall_back, R.id.slecall_giveup_tv, R.id.slecall_have_a_prescription_tv, R.id.slecall_no_prescription_tv})
    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {

            case R.id.slecall_back:
            case R.id.slecall_giveup_tv:
                startActivity(ShopMain_Aty.class, null);
                finish();
                break;

            case R.id.slecall_no_prescription_tv:
                consultation_type = 0;
                getUserSignCallDoctor();
                break;
            case R.id.slecall_have_a_prescription_tv:
                consultation_type = 1;
                getUserSignCallDoctor();

                break;
        }
    }


    public void getUserSignCallDoctor() {
        showProgressDialog("正在选择可问诊的医生，请等待...", false);
        try {
            JSONObject orderObject = new JSONObject();
            JSONArray product_list = new JSONArray();
            for (CarGoodsBeans dataBean : goodsBeansList) {
                JSONObject productobject = new JSONObject();
                productobject.put("productid", dataBean.getProductid());
                productobject.put("quantity", 1);
                JSONArray mSlotIndexs = new JSONArray(dataBean.getMslotIndexs());
                productobject.put("slotIndexs", mSlotIndexs);
                product_list.put(productobject);
            }
            orderObject.put("product_list", product_list);
            orderObject.put("order_id", shopOrderBean.getData().getId());
            orderObject.put("consultation_type", consultation_type);
            ShopApiHttp.handle_remote_order(orderObject.toString(),this);


        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    public void queryDoctor() {
        ShopApiHttp.query_doctor_online(this);
    }

    @Override
    public void onRequestStart(String requestUrl, int requestId) {

    }

    @Override
    public void onRequestComplete(String requestUrl, int requestId) {

    }

    @Override
    public void onSuccess(String requestUrl, JSONObject requestJson, int requestId) {
        if (requestUrl.endsWith(ShopApiHttp.handle_remote_order)) {
            queryDoctor();
            return;
        }
        if (requestUrl.endsWith(ShopApiHttp.query_doctor_online)) {
            removeProgressDialog();
            Bundle bundle = new Bundle();
            bundle.putString("order_id", shopOrderBean.getData().getId());
            bundle.putInt("consultation_type", 0);
            startActivity(CallPrescription_Aty.class, bundle);
            finish();
        }
    }

    @Override
    public void onError(String requestUrl, String errorMsg, String resultData, int requestId) {
        if (requestUrl.endsWith(ShopApiHttp.handle_remote_order)) {

            removeProgressDialog();
            showErrorToast(errorMsg);
            return;
        }
        if (requestUrl.endsWith(ShopApiHttp.query_doctor_online)) {
            removeProgressDialog();

            showErrorToast(errorMsg);
        }
    }
}
